package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author: lcc
 * @Desc: 瑞吉-自动填充处理器
 * @Date: 2022/6/5 21:45
 */
@Component
@Slf4j
public class ReggieMetaObjectHandler implements MetaObjectHandler {
    /**
     * 功能描述: 新增时执行自动填充功能
     * @param metaObject
     * @return : void
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = BaseContext.getUserId();
        Long currentId = BaseContext.getCurrentId();
        if(userId == null){
            userId= currentId;
        }
        log.info("ReggieMetaObjectHandler.insertFill当前线程的ID为：-------"+Thread.currentThread().getId()+"-----------"+currentId);
        log.info("公共字段自动填充[insert]...");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",userId);
        metaObject.setValue("updateUser",userId);
    }
    /**
     * 功能描述: 修改时执行自动填充功能
     * @param metaObject
     * @return : void
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = BaseContext.getUserId();
        Long currentId = BaseContext.getCurrentId();
        if(userId == null){
            userId= currentId;
        }
        log.info("ReggieMetaObjectHandler.updateFill当前线程的ID为：-------"+Thread.currentThread().getId()+"-----------"+currentId);
        log.info("公共字段自动填充[insert]...");
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",userId);
    }
}
